package com.sunnystudio.mydiary.data;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.airbiquity.android.fleet.icsobjs.IcsConstants;

public class ObjShiftAbstract {
	private int id = 0; // ID for local database
	
	private String shift_id = "";
	private long logon_time = 0;
	private long logoff_time = 0;
	private int driver_id_type = 0;
	private int obuId = 0;
	
	private String driver_id = "";
	private float total_distance_travelled = 0;
	private int total_duration = 0;
	private int idling_time = 0;
	
	private float over_rev_distance = 0;
	private float harsh_throttle_distance = 0;
	private float braking_distance = 0;
	private float cruise_control_distance = 0;
	
	private float coasting_distance = 0;
	
	private float harsh_cold_drv_distance = 0;
	private float eco_drive_distance = 0;
	private float harsh_acc_distance = 0;
	private float harsh_brake_distance = 0;
	private long crc = -1;
	
	/**
	 * None of the distances travelled parameters will exceed 5200km per shift.  
	 * Since each shift is by definition no more than 24 hours in length, 
	 * a driver would need to travel in excess of 216kph to over flow any of these distance travelled.  
	 * The total shift duration shoudn’t be more than 86400 seconds (24 hours), 
	 * and idling time shouldn’t be greater than shift duration.
	 * 
	 * @return
	 */
	public ObjShiftAbstract() {
		
	}
	
	/**
	 * Initialize with json string
	 * 
	 * @param String
	 *            json
	 */
	public ObjShiftAbstract(String json) {
		try {
			JSONObject obj = new JSONObject(json);
			init(obj);
		} catch (JSONException e) {
			Log.e("ObjShiftAbstract", "Json error: ", e);
		}
	}
	
	/**
	 * Initialize with json object
	 * @param obj
	 */
	public ObjShiftAbstract(JSONObject obj) {
		init(obj);
	}
	
	private void init(JSONObject obj) {
		this.shift_id = obj.optString(IcsConstants.KEY_SHIFT_ID);
		this.logon_time = obj.optLong(IcsConstants.KEY_REPORT_LOGON_TIME);
		this.logoff_time = obj.optLong(IcsConstants.KEY_REPORT_LOGOFF_TIME);
		this.driver_id_type = obj.optInt(IcsConstants.KEY_DRIVER_ID_TYPE);
		
		this.driver_id = obj.optString(IcsConstants.KEY_DRIVER_ID);
		this.total_distance_travelled = (float) obj.optDouble(IcsConstants.KEY_REPORT_DISTANCE);
		this.total_duration = obj.optInt(IcsConstants.KEY_REPORT_DURATION);
		this.idling_time = obj.optInt(IcsConstants.KEY_REPORT_IDLING);
		
		this.over_rev_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_OVER_REV);
		this.harsh_throttle_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_HARSH_THROTTLE);
		this.braking_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_BRAKING_DISTANCE);
		
		this.cruise_control_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_CRUISE);
		this.coasting_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_COASTING);
		
		this.harsh_cold_drv_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_HARSH_COLD);
		this.eco_drive_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_ECO_DRIVE);
		this.harsh_acc_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_HARSH_ACC);
		this.harsh_brake_distance = (float) obj.optDouble(IcsConstants.KEY_REPORT_HARSH_BRAKE);
		this.obuId = obj.optInt( IcsConstants.KEY_OBU_ID );
		this.crc = obj.optLong( IcsConstants.KEY_CRC );
	}
	
	public boolean isValid(){
		boolean isValid = true;
		
		if (this.shift_id == null) {
			isValid = false;
		}
		
		return isValid;
	}
	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	
	/**
	 * @return the obuId
	 */
	public int getObuId() {
		return obuId;
	}

	/**
	 * @param obuId the obuId to set
	 */
	public void setObuId(int obuId) {
		this.obuId = obuId;
	}

	/**
	 * @return the shift_id
	 */
	public String getShift_id() {
		return shift_id;
	}

	/**
	 * @param shift_id the shift_id to set
	 */
	public void setShift_id(String shift_id) {
		this.shift_id = shift_id;
	}

	/**
	 * @return the logon_time
	 */
	public long getLogon_time() {
		return logon_time;
	}

	/**
	 * @param logon_time the logon_time to set
	 */
	public void setLogon_time(long logon_time) {
		this.logon_time = logon_time;
	}

	/**
	 * @return the logoff_time
	 */
	public long getLogoff_time() {
		return logoff_time;
	}

	/**
	 * @param logoff_time the logoff_time to set
	 */
	public void setLogoff_time(long logoff_time) {
		this.logoff_time = logoff_time;
	}

	/**
	 * @return the driver_id_type
	 */
	public int getDriver_id_type() {
		return driver_id_type;
	}

	/**
	 * @param driver_id_type the driver_id_type to set
	 */
	public void setDriver_id_type(int driver_id_type) {
		this.driver_id_type = driver_id_type;
	}

	/**
	 * @return the driver_id
	 */
	public String getDriver_id() {
		return driver_id;
	}

	/**
	 * @param driver_id the driver_id to set
	 */
	public void setDriver_id(String driver_id) {
		this.driver_id = driver_id;
	}

	/**
	 * @return the total_distance_travelled
	 */
	public float getTotal_distance_travelled() {
		return total_distance_travelled;
	}

	/**
	 * @param total_distance_travelled the total_distance_travelled to set
	 */
	public void setTotal_distance_travelled(float total_distance_travelled) {
		this.total_distance_travelled = total_distance_travelled;
	}

	/**
	 * @return the total_duration
	 */
	public float getTotal_duration() {
		return total_duration;
	}

	/**
	 * @param total_duration the total_duration to set
	 */
	public void setTotal_duration(int total_duration) {
		this.total_duration = total_duration;
	}

	/**
	 * @return the idling_time
	 */
	public long getIdling_time() {
		return idling_time;
	}

	/**
	 * @param idling_time the idling_time to set
	 */
	public void setIdling_time(int idling_time) {
		this.idling_time = idling_time;
	}

	/**
	 * @return the over_rev_distance
	 */
	public float getOver_rev_distance() {
		return over_rev_distance;
	}

	/**
	 * @param over_rev_distance the over_rev_distance to set
	 */
	public void setOver_rev_distance(float over_rev_distance) {
		this.over_rev_distance = over_rev_distance;
	}

	/**
	 * @return the harsh_throttle_distance
	 */
	public float getHarsh_throttle_distance() {
		return harsh_throttle_distance;
	}

	/**
	 * @param harsh_throttle_distance the harsh_throttle_distance to set
	 */
	public void setHarsh_throttle_distance(float harsh_throttle_distance) {
		this.harsh_throttle_distance = harsh_throttle_distance;
	}

	/**
	 * @return the braking_distance
	 */
	public float getBraking_distance() {
		return braking_distance;
	}

	/**
	 * @param braking_distance the braking_distance to set
	 */
	public void setBraking_distance(float braking_distance) {
		this.braking_distance = braking_distance;
	}

	/**
	 * @return the cruise_control_distance
	 */
	public float getCruise_control_distance() {
		return cruise_control_distance;
	}

	/**
	 * @param cruise_control_distance the cruise_control_distance to set
	 */
	public void setCruise_control_distance(float cruise_control_distance) {
		this.cruise_control_distance = cruise_control_distance;
	}

	/**
	 * @return the coasting_distance
	 */
	public float getCoasting_distance() {
		return coasting_distance;
	}

	/**
	 * @param coasting_distance the coasting_distance to set
	 */
	public void setCoasting_distance(float coasting_time) {
		this.coasting_distance = coasting_time;
	}
	
	/**
	 * @return the harsh_cold_drv_distance
	 */
	public float getHarsh_cold_drv_distance() {
		return harsh_cold_drv_distance;
	}

	/**
	 * @param harsh_cold_drv_distance the harsh_cold_drv_distance to set
	 */
	public void setHarsh_cold_drv_distance(float harsh_cold_drv_distance) {
		this.harsh_cold_drv_distance = harsh_cold_drv_distance;
	}

	/**
	 * @return the eco_drive_distance
	 */
	public float getEco_drive_distance() {
		return eco_drive_distance;
	}

	/**
	 * @param eco_drive_distance the eco_drive_distance to set
	 */
	public void setEco_drive_distance(float eco_drive_distance) {
		this.eco_drive_distance = eco_drive_distance;
	}

	/**
	 * @return the harsh_acc_distance
	 */
	public float getHarsh_acc_distance() {
		return harsh_acc_distance;
	}

	/**
	 * @param harsh_acc_distance the harsh_acc_distance to set
	 */
	public void setHarsh_acc_distance(float harsh_acc_distance) {
		this.harsh_acc_distance = harsh_acc_distance;
	}

	/**
	 * @return the harsh_brake_distance
	 */
	public float getHarsh_brake_distance() {
		return harsh_brake_distance;
	}

	/**
	 * @param harsh_brake_distance the harsh_brake_distance to set
	 */
	public void setHarsh_brake_distance(float harsh_brake_distance) {
		this.harsh_brake_distance = harsh_brake_distance;
	}
	
	
	/**
	 * @return the crc
	 */
	public long getCrc() {
		return crc;
	}

	/**
	 * @param crc the crc to set
	 */
	public void setCrc(long crc) {
		this.crc = crc;
	}

	/**
	 * Convert object to json object
	 * 
	 * @return String
	 */
	public JSONObject toJsonObj() {
		JSONObject json = new JSONObject();
		try {			
			json.put(IcsConstants.KEY_ID, this.id);
			
			json.put(IcsConstants.KEY_SHIFT_ID, this.shift_id);
			json.put(IcsConstants.KEY_REPORT_LOGON_TIME, this.logon_time);
			json.put(IcsConstants.KEY_REPORT_LOGOFF_TIME, this.logoff_time);
			json.put(IcsConstants.KEY_DRIVER_ID_TYPE, this.driver_id_type);
			
			json.put(IcsConstants.KEY_DRIVER_ID, this.driver_id);
			json.put(IcsConstants.KEY_REPORT_DISTANCE, this.total_distance_travelled);
			json.put(IcsConstants.KEY_REPORT_DURATION, this.total_duration);
			json.put(IcsConstants.KEY_REPORT_IDLING, this.idling_time);
			
			json.put(IcsConstants.KEY_REPORT_OVER_REV, String.format("%.2f", this.over_rev_distance));
			json.put(IcsConstants.KEY_REPORT_HARSH_THROTTLE, String.format("%.2f", this.harsh_throttle_distance));
			json.put(IcsConstants.KEY_REPORT_BRAKING_DISTANCE, String.format("%.2f", this.braking_distance));
			json.put(IcsConstants.KEY_REPORT_CRUISE, String.format("%.2f", this.cruise_control_distance));
			
			json.put(IcsConstants.KEY_REPORT_COASTING, String.format("%.2f", this.coasting_distance));
			
			json.put(IcsConstants.KEY_REPORT_HARSH_COLD, String.format("%.2f", this.harsh_cold_drv_distance));
			json.put(IcsConstants.KEY_REPORT_ECO_DRIVE, String.format("%.2f", this.eco_drive_distance));
			json.put(IcsConstants.KEY_REPORT_HARSH_ACC, String.format("%.2f", this.harsh_acc_distance));
			json.put(IcsConstants.KEY_REPORT_HARSH_BRAKE, String.format("%.2f", this.harsh_brake_distance));
			json.put(IcsConstants.KEY_OBU_ID, this.obuId );
			json.put(IcsConstants.KEY_CRC, this.crc);
			
		} catch (JSONException e) {
			Log.e("ObjShiftAbstract", "Json error: ", e);
		}

		return json;
	}
	
	/**
	 * Convert object to json string
	 * 
	 * @return String
	 */
	public String toJsonString() {
		return toJsonObj().toString();
	}
}
